from flask import Flask, request, render_template
from datetime import datetime

app = Flask(__name__)


def date_format(value, format='%Y-%d-%m %H:%M'):
    return value.strftime(format)


app.add_template_filter(date_format, 'dformat')


class User:
    def __init__(self, username, email):
        self.username = username
        self.email = email


@app.route('/')
def hello_world():  # put application's code here
    user = User(username='知了', email='xx!@163.com')
    person = {
        'username': '张三',
        'email': 'zhangsan@qq.com'
    }
    return render_template('index.html', user=user, person=person)


@app.route("/blog/list")
def blog_list():
    return "blog列表"


# 带参数url
# @app.route("/blog/<int:blog_id>")
# def profile(blog_id):
#     return '您访问的博客是:%s' % blog_id

@app.route("/blog/<blog_id>")
def profile(blog_id):
    return render_template('blog_detail.html', blog_id=blog_id, username='xmf')


# 查询字符串方式
# /book/list: 返回第一页
# /book/list?page=2:返回第二页数据
@app.route('/book/list')
def book_list():
    page = request.args.get('page', default=1, type=int)
    return f'您获取的是第{page}页的图书列表'


@app.route('/filter')
def filter_demo():
    user = User(username='知了222', email='xx!@163.com')
    mytime = datetime.now()
    print(mytime)
    return render_template('filter.html', user=user, mytime=mytime)


@app.route('/control')
def contro_statement():
    age = 19
    books = [
        {
            'name': '三国演义',
            'author': '罗贯中'
        }, {
            'name': '水浒传',
            'author': '施耐庵'
        }
    ]
    return render_template('control.html', age=age,books=books)


if __name__ == '__main__':
    # app.run(debug=True, host='0.0.0.0', port=8080)
    app.run(debug=True)
